//
//  XGCollectionHeaderView.m
//  Instagram
//
//  Created by monkey on 2019/4/13.
//  Copyright © 2019 itcast. All rights reserved.
//

#import "XGCollectionHeaderView.h"

@interface XGCollectionHeaderView ()

/// 用户头像
@property (weak, nonatomic) IBOutlet UIImageView *userIconImageView;
/// 帖子数量
@property (weak, nonatomic) IBOutlet UILabel *topicCountLabel;
/// 粉丝数量
@property (weak, nonatomic) IBOutlet UILabel *fansCountLabel;
/// 关注数量
@property (weak, nonatomic) IBOutlet UILabel *focusCountLabel;
/// 用户昵称
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
/// 用户简介
@property (weak, nonatomic) IBOutlet UILabel *descriptionInfoLabel;
/// 性别
@property (weak, nonatomic) IBOutlet UILabel *genderLabel;
/// 编辑/关注按钮
@property (weak, nonatomic) IBOutlet UIButton *editOrFocusButton;

@end

@implementation XGCollectionHeaderView

#pragma mark - 解析模型

- (void)setUser:(AVUser *)user
{
    XGAccountViewModel *account = (XGAccountViewModel *)user;
    _userNameLabel.text = account.screenName;
    _descriptionInfoLabel.text = account.descriptionInfo;
    _genderLabel.text = account.gender == XGGenderTypeMale ? @"男" : @"女";
    DEBUG_Log(@"%@",account.userIcon);
    if (account.userIcon) {
        [XGImageManager imageForAVFile:account.userIcon placeholderImage:nil imageSize:CGSizeMake(80, 80) backgroundColor:[UIColor whiteColor] isUserIcon:YES completion:^(UIImage * _Nullable image) {
            self.userIconImageView.image = image;
        }];
    }
    
    // 帖子数
    AVQuery *topicQuery = [[AVQuery alloc] initWithClassName:@"Topic"];
    [topicQuery whereKey:@"user" equalTo:account];
    [topicQuery countObjectsInBackgroundWithBlock:^(NSInteger number, NSError * _Nullable error) {
        self.topicCountLabel.text = [NSString stringWithFormat:@"%zd",number];
    }];
    
    // 读取粉丝数目
    AVQuery *followerQuery = [[AVQuery alloc] initWithClassName:@"_Follower"];
    [followerQuery whereKey:@"user" equalTo:account];
    [followerQuery countObjectsInBackgroundWithBlock:^(NSInteger number, NSError * _Nullable error) {
        self.fansCountLabel.text = [NSString stringWithFormat:@"%zd",number];
    }];
    
    // 读取关注数目
    AVQuery *followeeQuery = [[AVQuery alloc] initWithClassName:@"_Followee"];
    [followeeQuery whereKey:@"user" equalTo:account];
    [followeeQuery countObjectsInBackgroundWithBlock:^(NSInteger number, NSError * _Nullable error) {
        self.focusCountLabel.text = [NSString stringWithFormat:@"%zd",number];
    }];
}

#pragma mark - 监听方法

/// 帖子数目点击事件
- (void)topicCountTapAction
{
    if ([_delegate respondsToSelector:@selector(collectionHeaderViewTopicCountDidClick:)]) {
        [_delegate collectionHeaderViewTopicCountDidClick:self];
    }
}

/// 粉丝数目点击事件
- (void)fansCountTapAction
{
    if ([_delegate respondsToSelector:@selector(collectionHeaderViewFansCountDidClick:)]) {
        [_delegate collectionHeaderViewFansCountDidClick:self];
    }
}

/// 关注数目点击事件
- (void)focusCountTapAction
{
    if ([_delegate respondsToSelector:@selector(collectionHeaderViewFocusCountDidClick:)]) {
        [_delegate collectionHeaderViewFocusCountDidClick:self];
    }
}

/// 编辑或关注按钮点击事件
- (IBAction)editOrFocusAction
{
    if ([_delegate respondsToSelector:@selector(collectionHeaderViewEditOrFocusDidClick:button:)]) {
        [_delegate collectionHeaderViewEditOrFocusDidClick:self button:_editOrFocusButton];
    }
}

#pragma mark - 其他方法

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    // label用户交互默认关闭
    _topicCountLabel.userInteractionEnabled = YES;
    _fansCountLabel.userInteractionEnabled = YES;
    _focusCountLabel.userInteractionEnabled = YES;
    
    // 添加手势
    UITapGestureRecognizer *topicCountTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topicCountTapAction)];
    UITapGestureRecognizer *fansCountTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fansCountTapAction)];
    UITapGestureRecognizer *focusCountTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(focusCountTapAction)];
    
    [_topicCountLabel addGestureRecognizer:topicCountTap];
    [_fansCountLabel addGestureRecognizer:fansCountTap];
    [_focusCountLabel addGestureRecognizer:focusCountTap];
}

- (UIButton *)editOrFocusButton
{
    return _editOrFocusButton;
}

@end

